package com.zbb.order.rabbitmq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zbb.order.config.DelayedQueueConfig;
import com.zbb.order.mapper.OrderInitiateMapper;
import com.zbb.order.mapper.OrderSnapshotMapper;
import com.zbb.order.model.po.OrderInitiate;
import com.zbb.order.model.po.OrderSnapshot;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class AddOrderConfirmConsumer {

 @Autowired
 OrderInitiateMapper orderInitiateMapper;

 @Autowired
 OrderSnapshotMapper orderSnapshotMapper;
 public static final String DELAYED_QUEUE_NAME = "delayed.queue";
 @RabbitListener(queues = DELAYED_QUEUE_NAME)
 public void receiveDelayedQueue(Message message){
  log.info("当前时间：{}，收到消息：{}",new Date().toString(),message.getBody().toString());
  //把订单状态设为过期
  OrderInitiate orderInitiate= orderInitiateMapper.selectById(message.getBody());
  if (orderInitiate.getStatus() == 0) {
   orderInitiate.setStatus(1);
   orderInitiateMapper.updateById(orderInitiate);
   QueryWrapper queryWrapper = new QueryWrapper();
   queryWrapper.eq("order_initiate_code", message.getBody());
   List<OrderSnapshot> orderSnapshots = orderSnapshotMapper.selectList(queryWrapper);
   OrderSnapshot orderSnapshot = orderSnapshots.get(0);
   orderSnapshot.setStatus(8);
   orderSnapshotMapper.updateById(orderSnapshot);
  }
 }

 @RabbitListener(queues = DelayedQueueConfig.DLK_QUEUE)
 public void receiveDelayedQueue1(Message message){
  log.info("当前时间：{}，收到死信消息发生发生发达的消息：{}",new Date().toString(),message.getBody().toString());
  //把订单状态设为过期
  OrderInitiate orderInitiate= orderInitiateMapper.selectById(message.getBody());
  if (orderInitiate.getStatus() == 0) {
   orderInitiate.setStatus(1);
   orderInitiateMapper.updateById(orderInitiate);
   QueryWrapper queryWrapper = new QueryWrapper();
   queryWrapper.eq("order_initiate_code", message.getBody());
   List<OrderSnapshot> orderSnapshots = orderSnapshotMapper.selectList(queryWrapper);
   OrderSnapshot orderSnapshot = orderSnapshots.get(0);
   orderSnapshot.setStatus(8);
   orderSnapshotMapper.updateById(orderSnapshot);
  }
 }
}